﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EzDesk.Core.Model;
using System.Reflection;

namespace EzDesk.Core.ServiceImpl.Repositories
{
    public static class ModelConvert
    {
        #region [ Others ]
        public static SimpleUser ToSimpleUser(this ez_UserProfile user)
        {
            if (user == null) return null;
            var model = new SimpleUser()
            {
                Id = user.UserId,
                Name = user.Name,
                UserName = user.UserName,
                Dept = user.ez_Dept.ToModel(),
            };
            return model;
        }
        #endregion

        #region To Model, LocalModel - IModel
        public static App ToModel(this ez_App app)
        {
            if (app == null) return null;
            App model = new App()
            {
                Id = app.AppId,
                Name = app.Name,
                Description = app.Description,
                Title = app.Title,
            };
            model.Domains = new List<AppUserAgentDomain>();
            foreach (var domain in app.ez_AppDomains)
            {
                model.Domains.Add(new AppUserAgentDomain()
                {
                    UserAgentStr = domain.UserAgentType,
                    Domain = domain.Domain,
                });
            }
            return model;
        }

        public static Dept ToModel(this ez_Dept dept)
        {
            if (dept == null) return null;
            Dept model = new Dept()
            {
                Id = dept.DeptId,
                DeptName = dept.DeptName,
                GroupId = dept.GroupId,
            };
            return model;
        }

        public static Group ToModel(this ez_Group group)
        {
            if (group == null) return null;
            Group model = new Group()
            {
                Id = group.GroupId,
                Description = group.Description,
                MemberString = group.MemberString,
                Type = group.Type,
                Name = group.Name,
            };
            return model;
        }

        public static Position ToModel(this ez_Position position)
        {
            if (position == null) return null;
            Position model = new Position()
            {
                Id = position.PosId,
                Description = position.Description,
                Priority = position.Priority
            };
            return model;
        }

        public static UserProfile ToModel(this ez_UserProfile user)
        {
            if (user == null) return null;
            UserProfile model = new UserProfile()
            {
                Id = user.UserId,
                UserCode = user.UserName,
                Name = user.Name,
                DeptId = user.DeptId,
                PositionId = user.PosId,
            };
            return model;
        }

        public static FeedTemplate ToModel(this ez_FeedTemplate template)
        {
            if (template == null) return null;
            FeedTemplate model = new FeedTemplate()
            {
                Id = template.TempleId,
                TemplateName = template.TemplateName,
                Body = template.Body,
                Title = template.Title,
                DefaultType = template.DefaultType,
                TemplateIconName = template.TemplateIcon,
            };
            return model;
        }

        public static Feed ToModel(this ez_Feed feed)
        {
            if (feed == null) return null;
            Feed model = new Feed()
            {
                CreateOn = feed.CreateOn,
                ExpireOn = feed.ExpireOn,
                Id = feed.FeedId,
                OwnUserId = feed.OwnerId.GetValueOrDefault(),
                TemplateId = feed.TemplateId,
                FeedData = feed.FeedData,
                UpdateOn = feed.UpdateOn,
                Icon = feed.Icon,
            };
            return model;
        }
        #endregion

        #region To Entity, IModel  -   LocalModel
        public static ez_App ToEntity(this App model)
        {
            if (model == null) return null;
            var entity = new ez_App
            {
                AppId = model.Id,
                Name = model.Name,
                Title = model.Title,
                Description = model.Description,
            };

            if (model.Domains != null)
            {
                entity.ez_AppDomains = new System.Data.Linq.EntitySet<ez_AppDomain>();
                foreach (var domain in model.Domains)
                {
                    entity.ez_AppDomains.Add(
                        new ez_AppDomain()
                        {
                            AppId = model.Id,
                            UserAgentType = domain.UserAgentStr,
                            Domain = domain.Domain,
                        });
                }
            }

            return entity;
        }

        public static ez_Dept ToEntity(this Dept model)
        {
            if (model == null) return null;
            return new ez_Dept
            {
                DeptId = model.Id,
                GroupId = model.GroupId,
                DeptName = model.DeptName
            };
        }

        public static ez_Group ToEntity(this Group model)
        {
            if (model == null) return null;
            return new ez_Group
            {
                Type = model.Type,
                MemberString = model.MemberString,
                Description = model.Description,
                GroupId = model.Id,
                Name = model.Name,
            };
        }

        public static ez_Position ToEntity(this Position model)
        {
            if (model == null) return null;
            return new ez_Position
            {
                Priority = model.Priority,
                Description = model.Description,
                PosId = model.Id,
            };
        }

        public static ez_FeedTemplate ToEntity(this FeedTemplate model)
        {
            if (model == null) return null;
            return new ez_FeedTemplate
            {
                TemplateName = model.TemplateName,
                DefaultType = model.DefaultType,
                Title = model.Title,
                TempleId = model.Id,
                Body = model.Body,
                TemplateIcon = model.TemplateIconName,
            };
        }

        public static ez_UserProfile ToEntity(this UserProfile model)
        {
            if (model == null) return null;
            return new ez_UserProfile
            {
                DeptId = model.DeptId,
                UserName = model.UserCode,
                UserId = model.Id,
                Name = model.Name,
                PosId = model.PositionId,
            };
        }

        public static ez_Feed ToEntity(this Feed model)
        {
            if (model == null) return null;
            return new ez_Feed
            {
                CreateOn = model.CreateOn,
                FeedData = model.FeedData,
                ExpireOn = model.ExpireOn,
                FeedId = model.Id,
                OwnerId = model.OwnUserId,
                TemplateId = model.TemplateId,
                UpdateOn = model.UpdateOn,
                Icon = model.Icon,
            };
        }
        

        #endregion

        #region Copy value from LocalModel to IModel

        public static void CopyValueApp(this ez_App app, App model)
        {
            try
            {
                app.AppId = model.Id;
                app.Name = model.Name;
                app.Description = model.Description;
                app.Title = model.Title;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void CopyValuePosition(this ez_Position position, Position model)
        {
            try
            {
                position.PosId = model.Id;
                position.Priority = model.Priority;
                position.Description = model.Description;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void CopyValueGroup(this ez_Group group, Group model)
        {
            try
            {
                group.Description = model.Description;
                group.GroupId = model.Id;
                group.MemberString = model.MemberString;
                group.Type = model.Type;
                group.Name = model.Name;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void CopyValueDept(this ez_Dept dept, Dept model)
        {
            try
            {
                dept.DeptName = model.DeptName;
                dept.DeptId = model.Id;
                dept.GroupId = model.GroupId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void CopyValueFeedTemplate(this ez_FeedTemplate template, FeedTemplate model)
        {
            try
            {
                template.TempleId = model.Id;
                template.TemplateName = model.TemplateName;
                template.Body = model.Body;
                template.Title = model.Title;
                template.DefaultType = model.DefaultType;
                template.TemplateIcon = model.TemplateIconName;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void CopyValueFeed(this ez_Feed update, Feed model)
        {
            try
            {
                update.FeedData = model.FeedData;
                update.CreateOn = model.CreateOn;
                update.ExpireOn = model.ExpireOn;
                update.OwnerId = model.OwnUserId;
                update.TemplateId = model.TemplateId;
                update.FeedId = model.Id;
                update.UpdateOn = model.UpdateOn;
                update.Icon = model.Icon;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public static void CopyValueUserProfile(this ez_UserProfile user, UserProfile model)
        {
            try
            {
                user.UserId = model.Id;
                user.UserName = model.UserCode;
                user.DeptId = model.DeptId;
                user.Name = model.Name;
                user.PosId = model.PositionId;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }



        #endregion Copy value from LocalModel to IModel


    }
}
